<?php
$post = $self->getPost();
$tags = App::object('Entity_Tag')->getCollection()->addPostCount();
$tags->getSelect()->having("tag.id in (select tag_id from post_tag where post_id = {$post->getId()})");
?>
<div class="post-tags">
	<h3>Tags</h3>
	
	<?php if (count($tags)): ?>
	<ul class="cur-tags">
		<?php foreach($tags as $tag): ?>
		<li>
		<a href="<?php echo App::url('post/index?tags='.$tag->getId()) ?>"><?php echo $tag->getName() ?></a>
		(<?php echo $tag->getPostCount() ?>)
		</li>
		<?php endforeach ?>
	</ul>
	<?php endif ?>

	<input type="text" id="txtTag" value="" />
	<button id="btnAddTag" class="button"><span><span>Add Tags</span></span></button>
	<a id="btnAllTag" href="#" title="Browse all tags">[...]</a>
	
	<div id="overlay" style="display: none"></div>
	<div id="tag-form" style="display:none">
		<button class="close" type="button">x</button>
		<div class="modal-header">
			<h3>Select tags</h3>
		</div>
		<div class="modal-body">
		<?php $tags = App::object('Entity_Tag')->getCollection(); ?>
		<ul class="all-tags">
			<?php foreach($tags as $tag): ?>
			<li>
			<label>
				<input type="checkbox" value="<?php $tag->getId() ?>" />
				<?php echo $tag->getName() ?>
			</label>
			</li>
			<?php endforeach ?>
		</ul>
		</div>
	</div>

	<script type="text/javascript">
	//<![CDATA[
	// create dialog
	var tagForm = new TagForm('tag-form', { 
		onHide: function() {
			$('#txtTag').val(this.getTags().join(', ')); 
		} 
	});

	// show dialog
	$('#btnAllTag').click(function (e) {
		tagForm.setTags($('#txtTag').val());
		tagForm.show();
		e.preventDefault();
	});
	
	// add tags
	$('#btnAddTag').click(function (e) {
		addTags(window.postId, $('#txtTag').val());
	});
	//]]>
	</script>
</div>